package com.magical.service.serviceImp;

import com.magical.dao.GradeMapper;
import com.magical.dao.UserMapper;
import com.magical.entity.User;
import com.magical.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Resource
    private GradeMapper gradeMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public User findByEmail(String email) {
        return userMapper.selectByEmail(email);
    }

    @Override
    public int addUser(String name, String email, String password) {
        User u=new User();
        u.setNickname(name);
        u.setPasswd(password);
        u.setEmail(email);
        userMapper.insertSelective(u);
        return u.getId();
    }
    @Override
    public List<User> selectAllUserMessage() {
        return userMapper.selectAllUserMessage();
    }

    @Override
    public int delete(int id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public User find(int id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public int update(int id, String email, int authority, int grade) {
        return userMapper.updateByPrimaryKey(id,email,authority,grade);
    }

    @Override
    public int usedSpace(int userId, int usedspace) {
        User user = userMapper.selectByPrimaryKey(userId);
        user.setUsedSpace(user.getUsedSpace()+usedspace);
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public int maxSpace(int gradeId) {
        return gradeMapper.selectByPrimaryKey(gradeId).getMaxSize();
    }
}